/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.office.notify.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.knet.sp.ncrm.common.config.Global;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.Encodes;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.office.notify.entity.OfficeNotify;
import cn.knet.sp.ncrm.modules.office.notify.service.OfficeNotifyService;

/**
 * 系统通知、预约提醒Controller
 * @author gehaiyang
 * @version 2015-07-20
 */
@Controller
@RequestMapping(value = "${adminPath}/office/notify/officeNotify")
public class OfficeNotifyController extends BaseController {

	@Autowired
	private OfficeNotifyService officeNotifyService;
	
	@ModelAttribute
	public OfficeNotify get(@RequestParam(required=false) String id) {
		OfficeNotify entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = officeNotifyService.get(id);
		}
		if (entity == null){
			entity = new OfficeNotify();
		}
		return entity;
	}
	
	@RequiresPermissions("office:officeNotify:view")
	@RequestMapping(value = {"list", ""})
	public String list(OfficeNotify officeNotify, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OfficeNotify> page = officeNotifyService.findPage(new Page<OfficeNotify>(request, response), officeNotify); 
		model.addAttribute("page", page);
		return "modules/office/notify/officeNotifyList";
	}
	
	@RequiresPermissions("office:officeNotify:view")
	@RequestMapping(value = "view")
	public String view(OfficeNotify officeNotify, Model model) {
		officeNotify.setContent(Encodes.unescapeHtml(officeNotify.getContent()));
		model.addAttribute("officeNotify", officeNotify);
		return "modules/office/notify/officeNotifyView";
	}

	@RequiresPermissions("office:officeNotify:view")
	@RequestMapping(value = "form")
	public String form(OfficeNotify officeNotify, Model model) {
		model.addAttribute("officeNotify", officeNotify);
		return "modules/office/notify/officeNotifyForm";
	}

	@RequiresPermissions("office:officeNotify:edit")
	@RequestMapping(value = "save")
	public String save(OfficeNotify officeNotify, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, officeNotify)){
			return form(officeNotify, model);
		}
		officeNotifyService.save(officeNotify);
		if(officeNotify.getStatus().equals("0")) {
			officeNotifyService.send(officeNotify);
			addMessage(redirectAttributes, "保存并发布通知成功");
		} else {
			addMessage(redirectAttributes, "保存通知成功");
		}
		return "redirect:"+Global.getAdminPath()+"/office/notify/officeNotify/?repage";
	}
	
	@RequiresPermissions("office:officeNotify:edit")
	@RequestMapping(value = "delete")
	public String delete(OfficeNotify officeNotify, RedirectAttributes redirectAttributes) {
		officeNotifyService.delete(officeNotify);
		addMessage(redirectAttributes, "删除通知成功");
		return "redirect:"+Global.getAdminPath()+"/office/notify/officeNotify/?repage";
	}

	@RequiresPermissions("office:officeNotify:send")
	@RequestMapping(value = "send")
	public String send(OfficeNotify officeNotify , RedirectAttributes redirectAttributes) {
		if(officeNotify.getStatus().equals("1")) {
			officeNotify.setStatus("0");
			officeNotifyService.save(officeNotify);//更新状态
		}
		officeNotifyService.send(officeNotify);
		addMessage(redirectAttributes, "发布通知成功");
		return "redirect:"+Global.getAdminPath()+"/office/notify/officeNotify/?repage";
	}
	
	
}